package org.blogsite.greencode.struts2.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.blogsite.greencode.bean.Schooling_Instructor_Course;
import org.blogsite.greencode.bean.TeachingSupport_Resource;
import org.blogsite.greencode.service.SchoolingManageService;
import org.blogsite.greencode.service.TeachingSupport_ResourceService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author：wzj E-mail: jsun789@gmail.com
 * @date：2010-7-25 下午07:10:58
 */
public class ResourceManageAction extends ActionSupport implements
		ServletContextAware {

	private static final long serialVersionUID = 1L;
	private static final String UPLOADPATH="/upload/";
	private File resource;
	private String resourceContentType;
	private String resourceFileName;
	private String fileSystemPath;
	private ServletContext context;
	private String searchText;
	private String searchRadio;
	private TeachingSupport_ResourceService teachingSupportResourceService;
	private SchoolingManageService schoolingManageService;
	private TeachingSupport_Resource teachingSupportResource;
	HttpServletRequest hs;
	
	public String download() throws Exception{
		System.out.println("download");
		return SUCCESS;
	}
	/*上传资源,保存到文件目录,相关信息保存到数据库*/
	public String upload() throws Exception {
		System.out.println("Upload");
		try {
			String fileName=teachingSupportResourceService
			.generateFileName(getResourceFileName());
			File target = new File(context.getRealPath(UPLOADPATH),fileName);
			FileUtils.copyFile(resource, target);
			hs = ServletActionContext.getRequest();
//			teachingSupportResource.setUserId(Integer.parseInt((String) hs.getSession().getAttribute("sid")));
			teachingSupportResource.setFileName(fileName);
			teachingSupportResource.setDate(new Date());
			/*调用服务层,添加资源*/
			teachingSupportResourceService.addResource(teachingSupportResource);
			hs.setAttribute("add","1");
			hs.setAttribute("addresource_success","1");
			
			
			// addResource(getResource(), fileName, getFileSystemPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "SUCCESS_SAVE";
	}

	public InputStream getDownloadFile() {
		try {

			return ServletActionContext.getServletContext()
					.getResourceAsStream(UPLOADPATH+ getResourceFileName());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// 显示资源查询
	public String displaySearch() throws Exception {

		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("searchtitle", "1");
		return "SUCCESS_DISPLAY";
	}
	/*显示资源添加页面*/
	public String displayAdd() throws Exception{
		hs = ServletActionContext.getRequest();
		// 调用服务层,获取所有课程的集合,显示在表单select,供用户选择
		hs.setAttribute("CourseNames", schoolingManageService
				.getAllCourseName());
		hs.setAttribute("add", "1");
		return "SUCCESS_DISPLAY";
	}
	
	// 查询资源
	public String search() throws Exception {
		List list;
		HttpServletRequest hs = ServletActionContext.getRequest();
		if (getSearchRadio().equals("all")||"".equals(getSearchRadio())||getSearchRadio()==null) {
			// 调用服务层,获取所有资源的集合
			list =teachingSupportResourceService.getAllResource();
			hs.setAttribute("allResource", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");

		} else if (getSearchRadio().equals("name")) {
			// 调用服务层,获取指定资源名称所有资源的集合
			try {

				list = teachingSupportResourceService.getResourceByName(getSearchText());
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				hs.setAttribute("allResource", list);
			} catch (Exception e) {
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("cname")) {
			// 调用服务层,获取指定课程名的所有资源的集合
			try {
				list = teachingSupportResourceService.getResourceByCname(getSearchText());
				hs.setAttribute("allResource", list);
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
			} catch (Exception e) {
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("blear")) {
			// 调用服务层,获取模糊查询所有资源的集合
			list =  teachingSupportResourceService.getResourceByBlear(getSearchText());
			hs.setAttribute("allResource", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");
		}
		hs.setAttribute("search", "1");
		return "SUCCESS_DISPLAY";
	}
	/*显示修改页面*/
	public String displayModify() throws Exception {

		hs = ServletActionContext.getRequest();
		
		// 调用服务层,获取所有课程的集合id,name,courseName,describe
		hs.setAttribute("CourseNames", schoolingManageService
				.getAllCourseName());
		
		hs.setAttribute("id", hs.getParameter("id"));
		hs.setAttribute("name", hs.getParameter("name"));
		hs.setAttribute("courseName", hs.getParameter("courseName"));
		hs.setAttribute("describe", hs.getParameter("describe"));
		hs.setAttribute("modify", "1");
		System.out.println("id:"+hs.getAttribute("id"));
		return "SUCCESS_DISPLAY";
	}  
	//修改资源信息
	public String modify() throws Exception{
		// 调用服务层,更新资源的信息
		
		teachingSupportResourceService.updateResource(teachingSupportResource);
		hs = ServletActionContext.getRequest();
		hs.setAttribute("modifysuccess", "1");
		return "SUCCESS_MODIFY";
	}
	
	//删除资源
	public String delete() throws Exception{
		hs = ServletActionContext.getRequest();
		try {
			System.out.println("eeee:"+hs.getParameter("id"));
			
			teachingSupportResourceService.deleteResource(new TeachingSupport_Resource(Integer.parseInt(hs.getParameter("id"))));
			File   file   =   new File(context.getRealPath(UPLOADPATH),hs.getParameter("fileName"));
			if(!file.isDirectory()   &&   file.exists()){ 
			    if(file.canWrite()) 
			        file.delete();
			    System.out.println("success delete");
			}
			hs = ServletActionContext.getRequest();
			hs.setAttribute("deletesuccess", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SUCCESS_DELETE";
	}
	/*显示删除资源*/
	public String displayDelete() throws Exception {
		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("delete","1");
		return "SUCCESS_DISPLAY";
	}
	/*修改之前,先查询*/
	public String beforeModify() throws Exception {
		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("beforemodify","1");
		return "SUCCESS_DISPLAY";
	}
	
	
	
	

	public ServletContext getServletContext() {
		return context;
	}

	public void setgetServletContext(ServletContext context) {
		this.context = context;
	}

	public File getResource() {
		return resource;
	}

	public void setResource(File resource) {
		this.resource = resource;
	}

	public String getResourceContentType() {
		return resourceContentType;
	}

	public void setResourceContentType(String resourceContentType) {
		this.resourceContentType = resourceContentType;
	}

	public String getResourceFileName() {
		return resourceFileName;
	}

	public void setResourceFileName(String resourceFileName) {
		this.resourceFileName = resourceFileName;
	}

	public String getFileSystemPath() {
		return fileSystemPath;
	}

	public void setFileSystemPath(String fileSystemPath) {
		this.fileSystemPath = fileSystemPath;
	}

	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	public TeachingSupport_ResourceService getTeachingSupportResourceService() {
		return teachingSupportResourceService;
	}

	public void setTeachingSupportResourceService(
			TeachingSupport_ResourceService teachingSupportResourceService) {
		this.teachingSupportResourceService = teachingSupportResourceService;
	}

	public SchoolingManageService getSchoolingManageService() {
		return schoolingManageService;
	}

	public void setSchoolingManageService(
			SchoolingManageService schoolingManageService) {
		this.schoolingManageService = schoolingManageService;
	}

	public TeachingSupport_Resource getTeachingSupportResource() {
		return teachingSupportResource;
	}

	public void setTeachingSupportResource(
			TeachingSupport_Resource teachingSupportResource) {
		this.teachingSupportResource = teachingSupportResource;
	}
	public String getSearchText() {
		return searchText;
	}
	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}
	public String getSearchRadio() {
		return searchRadio;
	}
	public void setSearchRadio(String searchRadio) {
		this.searchRadio = searchRadio;
	}
	
	

}
